package com.wc.alorithm_luogu._图论.请柬;

import java.util.*;

/**
 * @Author congge
 * @Date 2024/5/10 11:37
 * @description https://www.luogu.com.cn/problem/P1342
 */
public class Main {
    static Scanner sc = new Scanner(System.in);
    //    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1000010, idx = 1;
    static long INF = (long) 1e16;
    static int[] h = new int[N], rh = new int[N], e = new int[N], ne = new int[N], w = new int[N];
    static long[] dist = new long[N];
    static boolean[] st = new boolean[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c, h);
            add(b, a, c, rh);
        }
        long res = 0;
        spfa(h);
        for (int i = 2; i <= n; i++) res += dist[i];
        spfa(rh);
        for (int i = 2; i <= n; i++) res += dist[i];
        System.out.println(res);
        System.out.flush();
    }

    static void add(int a, int b, int c, int[] h) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void dijkstra(int[] h) {
        Arrays.fill(dist, 1, n + 1, INF);
        Arrays.fill(st, 1, n + 1, false);
        PriorityQueue<Pair> q = new PriorityQueue<>(Pair::compareTo);
        dist[1] = 0;
        q.add(new Pair(dist[1], 1));
        while (!q.isEmpty()) {
            int t = q.poll().y;
            if (st[t]) continue;
            st[t] = true;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    q.add(new Pair(dist[j], j));
                }
            }
        }
    }

    static void spfa(int[] h) {
        Arrays.fill(dist, 1, n + 1, INF);
        Arrays.fill(st, 1, n + 1, false);
        Queue<Integer> q = new LinkedList<>();
        q.add(1);
        dist[1] = 0;
        st[1] = true;
        while (!q.isEmpty()) {
            int t = q.poll();
            st[t] = false;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    if (!st[j]) {
                        q.add(j);
                        st[j] = true;
                    }
                }
            }
        }
    }

    static class Pair implements Comparable<Pair> {
        long x;
        int y;

        Pair(long x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(Pair o) {
            if (x == o.x) {
                return y - o.y;
            }
            return Long.compare(x, o.x);
        }
    }
}